<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>

<tiles:insertTemplate template="/layout/master.jsp">
  
    <tiles:putAttribute name="content">
    	<div class="loading" style="display:none;">
			<div class="loadingImg" ></div>
			<center>Carregando...</center>
		</div>
    	<div id="loginDialog" title='Login' style="display: none;"> 
    		
			<form action="${pageContext.request.contextPath}/authenticate" method="post">
				<table>
					<tr>
						<td>Login:</td>
						<td>
							<input type="text" name="user.login"/>
						</td>
					</tr>
					<tr>
						<td>Senha:</td>
						<td><input type="password" name="user.password"/></td>
				</table>
			</form>
		</div>
		<script>
			$(document).ready(function(){
				$( "#loginDialog" ).dialog({
					autoOpen: true,
					resizable: false,
					modal: true,
					width: "300px",
					closeOnEscape: false,
					open: function(event, ui) { $(".ui-dialog-titlebar-close").hide(); },
					buttons: {
						Login: function() {
							LoginPage.authenticate();
						}
					}
				});
				
				$('#loginDialog input').bind('keypress', function(e) {
			        if(e.keyCode==13){
			        	LoginPage.authenticate();
			        }
				});
			});	
			
			var LoginPage = {
				authenticate: function(){
					$("body").addClass("ui-state-disabled");
					$("#divContent").find(".loading").show();
					
					var form = $("#loginDialog form");
					var url = $("#loginDialog form").attr("action");
					
					$.post(url, form.serialize(),null,"json")
					.success(function(data) {
						window.location.href = window.location.href; 
					})
					.error(function(data){
						Messages.showValidations(data);
						
						$("body").removeClass("ui-state-disabled");
						$("#divContent").find(".loading").hide();
					});
				}
			};
		</script>
	</tiles:putAttribute>
</tiles:insertTemplate>